from flask import Flask,jsonify,request
from flask import Blueprint
from user import user
from config import config
from version import version
from getSql import getSql
from flask_jwt_extended import jwt_required
from flask_jwt_extended import JWTManager
from utils import *
from flask_cors import *

app = Flask(__name__)
CORS(app, supports_credentials=True)
app.register_blueprint(user, url_prefix='/user')
app.register_blueprint(config, url_prefix='/config')
app.register_blueprint(version, url_prefix='/version')
app.config["JWT_SECRET_KEY"] = "qinqingyyds"
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = False
jwt = JWTManager(app)

@jwt.invalid_token_loader
def invalid_token_callback(error):
    return jsonify({
        'msg':'请先登录',
        'code':'401'
    }), 401

@app.before_request
def before_request():
    try:
        token = request.headers.get('Authorization')
        # print(token)
        if isEmpty(token) == False:
            token = token.split(" ")[1]
            # print(token)
            cursor = getSql()
            sql = "select * from user where token = '%s'" % (token)
            cursor.execute(sql)
            data = cursor.fetchone()
            if data == None:
                print("身份失效")
                return jsonify({'msg':'身份失效','code':'401'}),401
    except Exception as e:
        print(e)
def main():
    app.run(host="0.0.0.0",port=5001)
if __name__ == '__main__':
    main()